package com.yugao.fintech.antelope.infra.weixin.mp.handler.scanbiz;

import com.alibaba.fastjson2.JSONObject;
import com.yugao.fintech.antelope.auth.AuthApi;
import com.yugao.fintech.antelope.auth.model.rqrs.third.LoginQrcodeInfo;
import com.yugao.fintech.antelope.infra.common.QrCodeSceneEnum;
import com.yugao.fintech.antelope.infra.common.ScanScene;
import com.yugao.fintech.framework.assistant.utils.StringUtils;
import lombok.extern.slf4j.Slf4j;
import me.chanjar.weixin.mp.bean.message.WxMpXmlMessage;
import org.springframework.stereotype.Component;

/**
 * 微信公众号扫码登录

 */
@Slf4j
@Component
public class WxMpQrCodeLoginHandler extends BaseWxScanEventHandler {
    @Autowired
    private AuthApi authApi;

    @Override
    protected void processBiz(WxMpXmlMessage message, ScanScene scanScene) {
        JSONObject params = scanScene.getParams();
        String qrCodeId = params.getString(ScanScene.QR_CODE_ID);
        String openId = message.getFromUser();
        String failReason = message.getFailReason();
        if (StringUtils.isNotEmpty(failReason)) {
            log.error("扫码登录失败, openId: {}, failReason: {}", openId, failReason);
            return;
        }

        LoginQrcodeInfo qrcodeInfo = new LoginQrcodeInfo();
        qrcodeInfo.setQrcodeId(qrCodeId).setOpenId(openId);
        authApi.userScanQrcode(qrcodeInfo);
    }

    @Override
    protected QrCodeSceneEnum scene() {
        return QrCodeSceneEnum.SCAN_LOGIN;
    }
}
